package com.example.usergroup.viewmodel;

import android.os.Bundle;

import androidx.databinding.ObservableField;

import com.example.mvvmcommon.field.UIField;
import com.example.mvvmcommon.mvvm.viewmodel.BaseViewModel;
import com.example.networkmoudel.entity.BaseEntity;
import com.example.usergroup.data.entity.UserEventEntity;
import com.example.usergroup.model.ForgetModel;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class ForgetPwdViewModel extends BaseViewModel<ForgetModel> {
    public ObservableField<String>mobile=new ObservableField<>();
    public ObservableField<String>verifyCode=new ObservableField<>();
    public ObservableField<String>getTv=new ObservableField<>();
    private Timer timer;
    private int count = 60;
    @Override
    protected void initData() {
        getTv.set("获取验证码");
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void update(UserEventEntity entity){
        if (entity.action.equals("finish")) {
            toBack();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected ForgetModel createModel() {
        return new ForgetModel();
    }

    @Override
    protected void sucessFull(BaseEntity entity) {
        if (entity.getStatus()==0){
//            ARouter.getInstance().build("/user/reset").navigation();
            Bundle bundle = new Bundle();
            bundle.putString("phone",mobile.get());
            Map<String,Object> map= new HashMap<>();
            map.put(UIField.ACTIONTYPE,UIField.AROUTERSTART);
            map.put(UIField.ACTIONROUTERKEY,"/user/reset");
            map.put(UIField.VALUESKEY,bundle);
            getUiChangeLiveData().getStartActivity().setValue(map);
        }
    }

    public void next(){
        Map<String,Object>map=new HashMap<>();
        map.put("mobile", mobile.get());
        map.put("verifyCode",verifyCode.get());
        m.requestForget(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    public void getVerifyCode(){
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                count--;
                getTv.set("倒计时:"+count+"秒");
                if (count==0){
                    getTv.set("获取验证码");
                    count=60;
                    timer.cancel();
                }
            }
        },0,1000);
        verifyCode.set("123456");
    }
    public void toBack(){
        getUiChangeLiveData().setFinishActivity(""+System.currentTimeMillis());
    }
}
